import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;


public class Test {
	
	public static void main(String[] args) {
		String url="http://vcdn01.icegaytube.tv/video8/2/25/267aa45c71bf999769dae71b3fa4f225.mp4?validfrom=1503931201&validto=1503976201&rate=951k&hash=qghUjWyJ1TTbZP1PATzAnVetLfc%3D";
		try {
			getImg(url);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void getImg(String imgUrl) throws ServletException, IOException {
	       /*if(StringUtil.isEmpty(imgUrl)){
	    	   //return null;
	       }
	       InternalResourceView iv=new InternalResourceView();
	       iv.setUrl(imgUrl);
	       iv.setAlwaysInclude(true);*/
	       
	      byte[] result= null;
	  	  InputStream in=null;
	  	  FileOutputStream os= null;
	  	URLConnection conn=null;
	  	  try {
		    	os=new FileOutputStream("E:/wang.mp4");
				URL realUrl = new URL(imgUrl);
				// 打开和URL之间的连接
				conn = realUrl.openConnection();
				// 设置通用的请求属性
				conn.setRequestProperty("accept", "*/*");
				conn.setRequestProperty("connection", "Keep-Alive");
				conn.setRequestProperty("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36");
				// 发送POST请求必须设置如下两行
				conn.setDoOutput(true);
				conn.setDoInput(true);
				// 定义BufferedReader输入流来读取URL的响应
				in = conn.getInputStream();
				result = new byte[1024];
				int ch = 0;
				while((ch=in.read(result))!=-1){
					os.write(result,0,ch);
				}
			} catch (Exception e) {
			}finally{
				in.close();
				os.close();
			}
	     
	     
	    }

	public static void main1(String[] args) {
		System.out.println("123456789".endsWith("90"));
		
		List<Map<String,Object>> staff=new ArrayList<Map<String,Object>>();
		staff.add(null);
		staff.set(0, null);
		
		BitSet bit=null;
		System.out.println(URLEncoder.encode("http://work.asiainfo.com/home/"));
		
		String[] s=new String[2];
		s[0]="123";
		System.out.println(StringUtils.join(s));
		System.out.println("123   sdfasdf".indexOf("23"));
		
	}
	
	public static void main0(String[] args) {
		System.out.println(Math.ceil(2.3));
		double d=5.0/2.0;
		System.out.println(Math.ceil(d));
		System.out.println((int)2.0);
	}
	
	public static void main1111(String[] args) {
		
		
		List<String> s=new ArrayList<String>();
		s.add("1");
		s.add("1");
		s.add("1");
		s.add("1");
		
		/*for(String n:s){
			if(n.equals("1")){
				s.add("2");
				//break;
			}
		}*/
		
		System.out.println("HN00003".matches("[a-zA-Z]{2,3}[0-9]{4,5}"));
		
		Calendar cal=Calendar.getInstance();
		String ss=null;
		ss="aaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
		//System.out.println(ss.toCharArray().length+":"+ss.length());
		getWH(ss);
		
		ss="大学四年，是思维打开了还是更加禁锢了？只能说相比四年前变化太多了，回到学校，想起";
		getWH(ss);
		
		//System.out.println(ss.toCharArray().length+":"+ss.length());
		ss="11111111111111111111111111111111111111111111111111111111111111111111";
		getWH(ss);
		getWH("");
		
		//System.out.println(ss.toCharArray().length+":"+ss.length());
	}
	
	public static void getWH1(String ss) {
		Graphics2D g = (Graphics2D)Toolkit.getDefaultToolkit().getImage("imgname").getGraphics();
				// 设置大字体
				Font font = new Font("微软雅黑", Font.LAYOUT_RIGHT_TO_LEFT | Font.BOLD, 12);
				g.setFont(font);
				FontRenderContext context = g.getFontRenderContext();
				// 获取字体的像素范围对象  
				Rectangle2D stringBounds = font.getStringBounds("text", context);
				double fontWidth = stringBounds.getWidth();
				System.out.println(fontWidth);
	}
	
	public static void getWH(String ss) {
		//Font font = new Font("Arial", Font.PLAIN, 12);
		Font font = new Font("微软雅黑", Font.CENTER_BASELINE, 16);
		FontMetrics metrics = new FontMetrics(font) {};
		Rectangle2D bounds = metrics.getStringBounds(ss, null);
		int widthInPixels = (int) bounds.getWidth();
		System.out.println(widthInPixels+":"+bounds.getHeight());
	}

}
